from datetime import datetime

from qturboframe.lib.report.log import logger
from qturboframe.lib.utils.by import By
from qturboframe.lib.utils.controlname import ControlName
from qturboframe.lib.ranorex.exceptions import CsClientError
from qturboframe.lib.ranorex.win_control.tab_page import TabPage as WinTabPage
from qturboframe.lib.utils.CustomError.custom_error import CustomError
from qturboframe.public_ui.__common__ import deal_form_data

from qturboframe.lib.ranorex.win_control.tab_page import TabPage as WinTabPage
from qturboframe.lib.ranorex.win_control.list import List as WinList

from qturboframe.public_ui.ucf_wpf.basecontrol import BaseControl
from qturboframe.public_ui.ucf_wpf.button import Button


class TabPage(BaseControl):
    """
    tab选项卡

    :Attributes:
     - _driver: 控件所在的当前Control对象
     - _control: 控件元素Control对象
     - tab_page: 当前的选项卡对象
     - timeout: 步骤操作的超时时间

     :Raises
        CsClientError 没有找到控件异常

    :Example:
     - 例1: TabPage({By.XPATH:"/DateTime"})
     - 例2: TabPage({By.AUTOMATIONID:'automationid'})
     - 例3: TabPage(control)
     - 例4: TabPage({By.TEXT: 'Click'}, father=client)
     - 例5: TabPage({By.AUTOMATIONID:'automationid'}, father=client)
     - 例6: TabPage(control, father=client)
    """

    def __init__(self, element_para=None, father=None, timeout=30000):
        if isinstance(element_para, dict) and By.TEXT in element_para:
            text = element_para.get(By.TEXT)
            self.tab_text = text
            xpath = ".//tabpage//text[@text='{}']".format(text)
            self.xpath = xpath
            text_control = father.find_first_visible_element(By.XPATH, xpath, timeout=timeout)
            if text_control:
                control = text_control.find_element_by_attribute(By.XPATH, ".//ancestor::tabpage", timeout=timeout)
                super().__init__(control, father, timeout)
            else:
                raise CsClientError(CustomError.Error_EQ03001000_Cannot_Find_Element,"没有找到此控件:{}".format(element_para))
        else:
            super().__init__(element_para, father, timeout)

        #转化为qturboframe.lib.ranorex.win_control.tab_page对象
        if self._control.get_control_name() == ControlName.TABPAGE:
            self.tab_page = WinTabPage(self._control.control)
        else:
            raise CsClientError(CustomError.Error_EQ03001004_InCorrect_Type,"请确定查找的控件是tab page控件")

    def tab_selected(self):
        '''
        选择选项卡

        :Example:
         - 例: tabs.select()
        '''
        if self.tab_page.is_visible():
            self.tab_page.click()
        else:
            raise CsClientError(CustomError.Error_EQ03001005_Not_Visible,"tab page不可见")

    def check_is_selected(self, expected_selected=1):
        '''
        判断选项卡是否被选中

        :Args:
         - expected_selected：选项卡期待的结果，1为期待选中

        :Example:
         - 例: tab.check_is_selected()
        '''
        actual_selected = self.tab_page.selected()
        if (expected_selected==1 and actual_selected) or (expected_selected==0 and not actual_selected):
            logger.info("{}选项卡为选中状态".format(self.tab_page.title()))
        else:
            raise CsClientError(CustomError.Error_EQ03010001_Verify_Expect_Actual_Not_Equal,"验证失败，期望的选项卡状态为:{}；实际选项卡的状态为:{}".format(expected_selected, actual_selected))

    def __get_button(self, text):
        '''
        ucf wpf 获取tab page中的按钮

        :Args:
         - text：按钮上的文本

        :Example:
         - 例: tab_page.get_button("确定")
        '''
        return Button({By.TEXT: text}, self._driver, self.timeout)

    def click_button(self, text='卖出[F2]'):
        '''
        ucf wpf 点击tab page上对应的按钮

        :Args:
         - text：按钮上的文本

        :Example:
         - 例: tab_page.click_button("确定")
        '''
        self.__get_button(text=text).click()

